package com.dshore.messagecenter.dao.system;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.system.SysLog;



@Repository
public class SysLogDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	/**
	 * 新增日志记录
	 * @param log
	 */
	public int insert(SysLog log) {
		String sql = "insert into T_SYS_LOG (ID,USER_NAME,USER_ID,CONTENT,CREATED_TIME) values (?, ?, ?,?,?)";
		return jdbcTemplate.update(sql,log.getId(), log.getUserName(), log.getUserId(),log.getContent(),log.getCreatedTime());
	}

	public List<SysLog> getPage(SysLog sysLog) {
		String stime=sysLog.getsTime();
		String etime=sysLog.geteTime();
		StringBuffer sql=new StringBuffer();
		List<Object> list=new ArrayList<>();
		sql.append(" select * from (");
		sql.append(" select A.*, rownum rn from (SELECT ID,USER_NAME,USER_ID,CONTENT,CREATED_TIME FROM T_SYS_LOG WHERE DEL_FLAG=0");
		if(!StringUtils.isEmpty(sysLog.getUserName())) {
			sql.append(" AND USER_NAME LIKE ?");
			list.add("%"+sysLog.getUserName()+"%");
		}
		if(!StringUtils.isEmpty(stime)&&!StringUtils.isEmpty(etime)) {
			stime=stime+" 00:00:00";
			etime=etime+" 23:59:59";
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') >=?");
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') <=?");
			list.add(stime);
			list.add(etime);
		}else if(!StringUtils.isEmpty(stime)) {
			stime=stime+" 00:00:00";
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') >=?");
			list.add(stime);
		}else if(!StringUtils.isEmpty(etime)) {
			etime=etime+" 23:59:59";
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') <=?");
			list.add(etime);
		}
			sql.append(" order by CREATED_TIME desc) A   ) where rn between ? and ? ");
			list.add(sysLog.getOffset()+1);
			list.add(sysLog.getOffset()+sysLog.getLimit());
			Object[]args=list.toArray();
		RowMapper<SysLog> rowMapper = new BeanPropertyRowMapper<>(SysLog.class);
		return jdbcTemplate.query(sql.toString(), args, rowMapper);
	}

	public long getPageCount(SysLog sysLog) {
		String stime=sysLog.getsTime();
		String etime=sysLog.geteTime();
		List<Object> list=new ArrayList<>();
		StringBuffer sql=new StringBuffer("SELECT COUNT(1) FROM T_SYS_LOG WHERE DEL_FLAG=0");
		if(!StringUtils.isEmpty(sysLog.getUserName())) {
			sql.append(" AND USER_NAME LIKE ?");
			list.add("%"+sysLog.getUserName()+"%");
		}
		if(!StringUtils.isEmpty(stime)&&!StringUtils.isEmpty(etime)) {
			stime=stime+" 00:00:00";
			etime=etime+" 23:59:59";
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') >=?");
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') <=?");
			list.add(stime);
			list.add(etime);
		}else if(!StringUtils.isEmpty(stime)) {
			stime=stime+" 00:00:00";
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') >=?");
			list.add(stime);
		}else if(!StringUtils.isEmpty(etime)) {
			etime=etime+" 23:59:59";
			sql.append(" AND TO_CHAR(CREATED_TIME,'yyyy-mm-dd hh24:mi:ss') <=?");
			list.add(etime);
		}
		Object[]args=list.toArray();
		return jdbcTemplate.queryForObject(sql.toString(),args, Long.class);
	}


	public List<Map<String, Object>> selectData(String tableName, String fieldName, Object[] ids) {
		StringBuffer sql=new StringBuffer("SELECT ");
		sql.append(fieldName);
		sql.append(" FROM ");
		sql.append(tableName);
		sql.append(" where id in(");
		for(int i=0;i<ids.length;i++) {
			sql.append("?");
			if(i!=ids.length-1) {
				sql.append(",");
			}
		}
		sql.append(")");
		try {
			return jdbcTemplate.queryForList(sql.toString(), ids);
		} catch (Exception e) {
			return null;
		}
		
	}

	public String selectDictName(String code,String type) {
		String sql="SELECT DICT_NAME FROM T_SYS_DICT WHERE DICT_TYPE=? AND DICT_CODE=?";
		return jdbcTemplate.queryForObject(sql.toString(), String.class,type,code);
	}

	public List<Map<String, Object>> selectData(String tableName, String fieldName,String queryArgs, Object[] args) {
		StringBuffer sql=new StringBuffer("SELECT ");
		sql.append(fieldName);
		sql.append(" FROM ");
		sql.append(tableName);
		sql.append(" where ");
		sql.append(queryArgs);
		sql.append(" in(");
		for(int i=0;i<args.length;i++) {
			sql.append("?");
			if(i!=args.length-1) {
				sql.append(",");
			}
		}
		sql.append(")");
		try {
			return jdbcTemplate.queryForList(sql.toString(), args);
		} catch (Exception e) {
			return null;
		}
		
	}
	
	public List<Map<String, Object>> selectData(Object[] ids) {
		StringBuffer sql=new StringBuffer("SELECT");
		sql.append(" KPI_ID,OBJ_TYPE");
		sql.append(" FROM T_MR_WARNING_THRESHOLD");
		sql.append(" where ID");
		sql.append(" in(");
		for(int i=0;i<ids.length;i++) {
			sql.append("?");
			if(i!=ids.length-1) {
				sql.append(",");
			}
		}
		sql.append(")");
		try {
			System.out.println(sql.toString());
			return jdbcTemplate.queryForList(sql.toString(), ids);
		} catch (Exception e) {
			return null;
		}
		
	}
}
